#UISwitcher.py -- allows seamless switching between UI screens
from UI import *
storage = dict()
def saveUI(uitype,ui):#save a UI
	storage[uitype] = ui
def loadUI(uitype):#load a UI
	if (not storage.__contains__(uitype)):
		print "Error, UI not found!"
	Game()[A_UI_READER] = storage.pop(uitype)

def SwitchUI(toui,otherargs = None,otherargs2 = None):#switch the ui
	if (otherargs == "\\LOADGAME"):#immediately loads the game
		pass
	currentui = Game()[A_UI_READER]
	currentuitype = currentui.getUIConstant()
	if (currentuitype == UI_CHAR):#undo the ai override if its from a character
		currentui.creature.UndoAIOverride()
	if (toui == UI_TITLE):#title screen
		Game()[A_UI_READER] = TitleScreen()
	elif (toui == UI_KEY_BIND):#keybindings screen
		Game()[A_UI_READER] = keyBindUI()
	elif (toui == UI_SELECTOR):
		saveUI(UI_MAP,Game()[A_UI_READER])#save the mapui
		Game()[A_UI_READER] = SelectionUI()
	elif (toui == UI_LOAD):#load screen
		saves = os.listdir(SAVEFILE_PATH)#see if we can load any files
		realsavefiles = []
		for save in saves:
			if (save[len(save)-4:] == ".sav"):#load each savefile
				realsavefiles.append(save)
		if (len(realsavefiles) == 0):#if there are no savefiles
			PauseConsoleErrorScreen("There are no savefiles to load.\nTo start a new game, press Start Game.")
			SwitchUI(UI_TITLE)#recursive
			return
		Game()[A_UI_READER] = LoadScreen(saves)#load the files
	elif (toui == UI_MAP):#map screen
		if (otherargs == "\\RESTORE"):#explicit restore command
			loadUI(UI_MAP)
		elif (otherargs != None):#load file
			if (Game().load(otherargs)):#if loading was successful
				Game()[A_UI_READER] = MapUI()#switch
		else:#empty file
			Game()[A_MAP] = Map(MAP_WIDTH,MAP_HEIGHT)
			Game()[A_MAP_VIEW] = MapViewer(Game()[A_MAP])
			Game()[A_UI_READER] = MapUI()
		if (Game()[A_MAP_VIEW] != None):#display the view if available
			Game()[A_MAP_VIEW].displayView()
	elif (toui == UI_CHAR):#character screen
		saveUI(UI_MAP,Game()[A_UI_READER])#save the mapui
		Game()[A_UI_READER] = CreatureUI(otherargs)
